package com.genius.util.ControllerHelper;

import com.genius.util.ConstPool.illuminatorPool;
import com.genius.util.Exception.illuminatorStatus;
import com.genius.util.Utils.DateHelper;
import com.genius.util.Utils.HTTP.FileSuperMan;
//import com.genius.app.bean.File;
//import com.genius.app.config.Result;
//import com.genius.app.service.FileService;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class FileControllerHelper extends Helper{

//    public Result checkDataBase(List<File> sqlFileList, String username, String fileName, File uploadFile, FileService fileService){
//        boolean iHaveThisFile = false;
//        java.io.File javaFile = null;
//        for(File file:sqlFileList){
//            if(file.getFileUserId().equals(username)){
//                iHaveThisFile = true;
//            }
//        }
//        File temp = sqlFileList.get(0);
//        if(!iHaveThisFile){
//            uploadFile = temp;
//            uploadFile.setFileUserId(username);
//            uploadFile.setFileName(fileName);
//            uploadFile.setFileCreateDate(DateHelper.formatYMDHMS(false));
//            fileService.addByFile(uploadFile,false);
//            if((javaFile=new java.io.File(temp.getFilePath())).exists()){
//                illuminatorPool.fileMap.put(temp.getFileId(),javaFile);
//            }
//            return Result.error(illuminatorStatus.FileQuickUpload);
//        }else{
//            if((javaFile=new java.io.File(temp.getFilePath())).exists()){
//                illuminatorPool.fileMap.put(temp.getFileId(),javaFile);
//            }
//            return Result.error(illuminatorStatus.FileExist);
//        }
//    }

    public String getFile_id(int slice, Map<String, String> formFiled, InputStream inputStream)throws IOException {
        String md5 = null;
        if(slice==0){
            md5 = FileSuperMan.md5File(inputStream);
        }else{
            if((md5=checkFormFiled(formFiled,"file_id"))!=null){
                return md5;
            }
        }
        return md5;
    }


    public java.io.File checkAndMerge(String md5,String lengthString,String fileName){
        String path = illuminatorPool.uploadFileTempPath+lengthString+"_"+md5;
        java.io.File javaFile = FileSuperMan.getDirectory(path);
        java.io.File resFile = null;
        if(javaFile!=null){
            java.io.File files[] = javaFile.listFiles();
            if(files!=null&&files.length==Integer.parseInt(lengthString)&&fileName!=null&&!fileName.trim().equals("")){
                int length = Integer.parseInt(lengthString);
                resFile = FileSuperMan.mergeFiles(path,fileName,md5,length);
                //resFile = FileSuperMan.mergeFilesByInputStream(path,fileName,md5,Integer.getInteger(lengthString));
            }
        }
        return resFile;
    }
}
